﻿using System.Web.Mvc;
using Complaint.Models;

namespace Complaint.Helpers
{
    public class ViewModelWithDataList<T, C, CM> : IViewModel<T>
        where T : class, new()
        where C : class, new()
        where CM : IModel<C>, new()
    {
        public T Instance { get; set; }

        public SelectList PickList { get; private set; }

        public string TextField { get; private set; }

        public string ValueField { get; private set; }

        public ViewModelWithDataList(T model, string textField, string valueField)
        {
            Instance = model;
            TextField = textField;
            ValueField = valueField;

            using (new UnitOfWorkScope(false))
            {
                var m = new CM();
                var list = m.Retrieve();
                list.Insert(0, new C());
                PickList = new SelectList(list, TextField, ValueField);
            }
        }
    }
}
